package com.tianji.promotion.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Configuration
public class PromotionConfig {

    /**
     * 异步生成优惠券兑换码线程池
     * @return
     */
    @Bean
    public Executor generateExchangeCodeExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //1、线程池核心数量
        executor.setCorePoolSize(2);
        //2、线程池最大线程数
        executor.setMaxPoolSize(5);
        //3、线程池等待队列大小
        executor.setQueueCapacity(50);
        //4、线程名称前缀
        executor.setThreadNamePrefix("exchange-code-handler-");
        //5、拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //6、初始化
        executor.initialize();
        //7、返回
        return executor;
    }
}
